﻿/*********************************************************************************************************************
Unit tests VanguardDocumentation, Message 740900-740999

© 2014 Erik Ackerholm

This file is part of SqlVanguard.

SqlVanguard is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later 
version.

SqlVanguard is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with SqlVanguard. If not, see 
<http://www.gnu.org/licenses/>.
*********************************************************************************************************************/

set nocount on;
set xact_abort on;
GO



-- Throwing errors
begin try;
	raiserror (740900, 16, 1, 'Unit test', 111, 222, 'Original message');
end try
begin catch;
	if error_number() <> 740900
		raiserror ('Missing or erroneous message for 740900', 16, 1);
end catch;
GO

begin try;
	raiserror (740901, 16, 1, 'Input.name.or.type');
end try
begin catch;
	if error_number() <> 740901
		raiserror ('Missing or erroneous message for 740901', 16, 2);
end catch;
GO

begin try;
	raiserror (740902, 16, 1, 'ToParse', 'some');
end try
begin catch;
	if error_number() <> 740902
		raiserror ('Missing or erroneous message for 740902', 16, 3);
end catch;
GO

begin try;
	raiserror (740903, 16, 1, 'ToParse');
end try
begin catch;
	if error_number() <> 740903
		raiserror ('Missing or erroneous message for 740903', 16, 4);
end catch;
GO

begin try;
	raiserror (740904, 16, 1, 5);
end try
begin catch;
	if error_number() <> 740904
		raiserror ('Missing or erroneous message for 740904', 16, 5);
end catch;
GO

begin try;
	raiserror (740905, 16, 1, 2, 'SCHEMA.TYPE');
end try
begin catch;
	if error_number() <> 740905
		raiserror ('Missing or erroneous message for 740905', 16, 6);
end catch;
GO

begin try;
	raiserror (740906, 16, 1, 'UNKNOWN', 0);
end try
begin catch;
	if error_number() <> 740906
		raiserror ('Missing or erroneous message for 740906', 16, 7);
end catch;
GO

begin try;
	raiserror (740907, 16, 1, '.SUFFIX');
end try
begin catch;
	if error_number() <> 740907
		raiserror ('Missing or erroneous message for 740907', 16, 7);
end catch;
GO



-- Number of substitution parameters
declare @ExpectedSubstitutions as table (
	MessageID    integer not null,
	Strings      tinyint not null,
	Integers     tinyint not null,
	primary key (MessageID)
);

declare @MessageID as integer;

insert into @ExpectedSubstitutions
values
	(740900, 2, 2), (740901, 1, 0), (740902, 2, 0), (740903, 1, 0), (740904, 0, 1),
	(740905, 1, 1), (740906, 1, 1), (740907, 1, 0);

with
	ActualSubstitutions as (
		select message_id,
			len([text]) - len(replace([text], '%s', 's')) as Strings,
			len([text]) - len(replace([text], '%i', 'i')) as Integers
		from sys.messages
		where message_id between 740900 and 740999
			and language_id = 1033
	)
select top (1) @MessageID = asb.message_id
from ActualSubstitutions as asb
	inner join @ExpectedSubstitutions as esb on (asb.message_id = esb.MessageID)
where asb.Strings <> esb.Strings or asb.Integers <> esb.Integers;

if
	@MessageID is not null
begin
	raiserror ('Too many or too few substitutions for message %i', 16, 1, @MessageID);
end;
GO




